/**
	Copyright (c) 2009 Ozazo Pvt Ltd, INDIA

	Permission is hereby granted, free of charge, to any person or organization 
	obtaining a copy of this software and associated  documentation  files (the 
	"Software"), to deal in the Software without restriction, including without 
	limitation the rights to use, copy, modify, merge, publish, distribute, sub
	license, and/or sell copies of the Software, and to permit persons  to whom 
	the Software is furnished to do so, subject to the following conditions:
	
	The above copyright notice and  this permission notice shall be included in 
	all copies or substantial portions of the Software.
	
	THE SOFTWARE IS PROVIDED "AS IS",  WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING  BUT NOT  LIMITED  TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR  COPYRIGHT  HOLDERS  BE  LIABLE  FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY,  WHETHER  IN  AN  ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
	FROM,  OUT  OF  OR  IN  CONNECTION  WITH  THE  SOFTWARE OR THE USE OR OTHER 
	DEALINGS IN THE SOFTWARE
 */

package com.ozazo.wave.robots.fw.engines;

/**
 * @author Araf Karsh Hamid
 * @version 1.0
 * @seriel 20091121102030
 *
 */
public class Commands {
	
	/**
	 * Following variables are final and needs to be SET using the constructor.
	 * It should NOT be modified at a later stage.
	 */
	private final String text;
	private final String cmd;
	private final String value;
	private final String oValue;
	private final String oCmd;
	private final int length;
	
	// Query Result Variables to store the data and status of the Query
	private boolean foundResult = false;	
	private Object data = null;
	
	/**
	 * Create the Commands Object with parameters and its values.
	 * 
	 * @param String _text
	 * @param String _cmd
	 * @param String _oCmd
	 * @param String _value
	 * @param String _oValue
	 */
	public Commands(final String _cmd, final String _oCmd, final String _value, String _oValue) {
		// text	= (_text != null)	? _text		: "NO_TXT";
		cmd		= (_cmd != null)	? _cmd		: "NO_CMD";
		oCmd	= (_oCmd != null)	? _oCmd		: "NO_OCMD";
		value	= (_value != null)	? _value	: "NO_VAL";
		oValue	= (_oValue != null) ? _oValue	: "NO_OVAL";
		
		text	= new StringBuffer().append(oCmd).append(" ").append(oValue).toString();

		int tLength = oCmd.length() + 1;
		if(oValue.startsWith("\"") || oValue.startsWith("'") ) {
			tLength += 1;
		}
		length = tLength;
	}
	
	/**
	 * Returns TRUE if the Search Query found any result and the result is set into the Command Object.
	 * 
	 * @return boolean
	 */
	
	public boolean foundResult() {
		return foundResult;
	}
	
	/**
	 * Gets the text matched by the Pattern.
	 * 
	 * @return String
	 */
	
	public String getText() {
		return text;
	}
	
	/**
	 * Gets the command without any / or - 
	 * For Example
	 * w   for Wikipedia Search
	 * wd  for Wikipedia Search Result Description
	 * ltx LaTeX Command to build the formula image
	 * 
	 * @return String
	 */
	
	public String getCommand() {
		return cmd;
	}

	/**
	 * Get the Original Commands. For Example
	 * /w   for Wikipedia Search
	 * /wd  for Wikipedia Search Result Description
	 * /ltx LaTeX Command to build the formula image
	 * 
	 * @return
	 */
	public String getOriginalCommand() {
		return oCmd;
	}
	
	/**
	 * Get the value for the command.
	 * 
	 * @return String
	 */
	
	public String getValue() {
		return value;
	}
	
	/**
	 * Get the Original Value. This value may contain single or double quotes.
	 * 
	 * @return String
	 */
	
	public String getOriginalValue() {
		return oValue;
	}
		
	/**
	 * Get the Query Result.
	 * Eg. A Wikipedia Search Result or a LaTeX query result
	 * 
	 * @return Object
	 */
	public Object getExternalData() {
		return data;
	}	

	/**
	 * Returns the length to be subtracted after replacing the Original text
	 * with a Link.
	 * 
	 * @return int
	 */
	public int getLengthToBeSubtracted() {
		return length;
	}

	/**
	 * If no Result found after any search. Call this method to notify other Views.
	 */
	public void setNoResult() {
		setExternalData(null);
	}

	/**
	 * Set the Result data after query based on the command and its value.
	 * 
	 * @param Object _o
	 */
	public void setExternalData(Object _o) {
		if(_o == null) {
			foundResult = false;
			return;
		}
		data = _o;
		foundResult = true;
	}

	/**
	 * HashCode of the matched Text
	 * 
	 * @return int
	 */
	
	public int hashCode() {
		return toString().hashCode();
	}

	/**
	 * Returns the matched Text
	 * 
	 * @return String
	 */
	public String toString() {
		return text;
	}

	/**
	 * Compares two Commands Object
	 * 
	 * @return boolean
	 */
	
	public boolean equals(Object o) {
		try {
			Commands c = (Commands)o;
			return (c.toString().equals(this.toString())) ? true : false;
		} catch (Exception ignored) {}
		return false;
	}
}